<?php

namespace App\Services\Stats\User\Elasticsearch;

use App\Core\Traits\Numeral;
use App\Models\Elasticsearch\Log\Login as Log;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use Helper\Timed;

class EsPlayerService extends Service
{
	use DatabaseElasticsearch, Numeral;

	private function makeAggParams($request)
	{
		$datetime = $this->getParamArray($request, 'datetime');
		list($start_time, $end_time) = Timed::datetimeFormat1($request,'datetime', $datetime[0], $datetime[1]);

		return [
			'body' => [
				'size' => 0,
				'aggs' => [
					'group_time' => [
						'date_histogram' => [
							'field' => 'end_time_at',
							'time_zone' => Timed::getTimeCode(),
							'calendar_interval' => '1d',
							'format' => 'yyyy-MM-dd',
							'min_doc_count' => 0,
							'extended_bounds' => [
								'min' => $start_time * 1000,
								'max' => $end_time * 1000,
							],
						],
						'aggs' => [
							'time_stats' => [
								'cardinality' => [
									'field' => 'uid',
									'precision_threshold' => 40000
								]
							],
						],
					],
				]
			]
		];
	}
	/**
	 * 获取统计数据
	 *
	 * @param \Illuminate\Http\Request $request
	 * @param \Illuminate\Database\Query\Builder $query
	 * @param int $timed
	 * @param int $timedEnd
	 * @return array
	 */
	public function u_game($request, $group_time, $start_time, $end_time)
	{
		//1.游戏人数
		$params = ['body' => ['size' => 0]];
		// 筛选【状态】
		$this->makeWhere($params, 'status', SettlementInfo::STATUS_NORMAL);
		// 筛选【类型】
		$this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
		// 筛选【平台】
		$this->makeWhereParamOrg($request, $params);
		// 筛选【时间】
		$this->makeWhereParamDatetime1($request, $params, 'end_time', 'datetime',null, $start_time, $end_time);
		// 列表数据
		$this->makeGroupDatetimeAggs($request, $params, 'sum', [], [], "group_str", $group_time);
		$params['body']['aggs']['group']['aggs'] = array_merge($params['body']['aggs']['group']['aggs'], $this->makeDistinct('uid'));
		$this->makeAggsOrderParam($request, $params, '_key');
		$aggStat = SettlementInfo::search($params);
		// 统一返回数据格式
		date_default_timezone_set(Timed::getTimezoneName());

		$result = [];
		foreach ($aggStat['aggregations']['group']['buckets'] as $bucket){
			$result[] = [
				'u_game' => $bucket['distinct_uid']['value'],
				'timed' => $group_time == '1w' ? date('W', $bucket['key']['timed'] / 1000) : $bucket['key']['timed'] / 1000,
			];
		}

		return $result;
	}


	public function u_sign($request, $group_time, $start_time, $end_time)
	{
		Timed::setForceAbs();
		//1.游戏人数 u_sign
		$params = ['body' => ['size' => 0]];
		// 筛选【平台】
		$this->makeWhereParamOrg($request, $params);
		// 筛选【时间】
		$this->makeWhereParamDatetime1($request, $params, 'login_time', 'datetime', null, $start_time, $end_time);
		// 列表数据
		$params['body']['aggs'] = [
			'group_by_day' => [
				'date_histogram' => [
					'field' => 'login_time_at',
					'calendar_interval' => $group_time,
					'format' => 'yyyy-MM-dd',
					'time_zone' => Timed::getTimeCode(),
					'min_doc_count' => 0,
					'extended_bounds' => [
						'min' => $start_time * 1000,
						'max' => $end_time  * 1000,
					]
				],
				'aggs' => $this->makeDistinct('uid')
			]
		];
		$aggStat = Log::search($params);
		// 统一返回数据格式
		date_default_timezone_set(Timed::getTimezoneName());
		$result = [];
		foreach ($aggStat['aggregations']['group_by_day']['buckets'] as $bucket){

			if ($group_time == '1w') {
				$result[] = [
					'u_sign' => $bucket['distinct_uid']['value'],
					'timed'  => date('W', strtotime($bucket['key_as_string'])),
				];
			} elseif ($group_time == '1M') {
				$result[] = [
					'u_sign' => $bucket['distinct_uid']['value'],
					'timed'  => date('Y.m', strtotime($bucket['key_as_string'])),
				];
			} else {
				$result[] = [
					'u_sign' => $bucket['distinct_uid']['value'],
					'timed'  => $bucket['key'] / 1000,
				];
			}
		}

		return $result;
	}
}
